package cn.xiaolang.function.data_structure;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 构建一个上下文（线程上下文的设计模式）
 *
 * @author fuhw/Dean
 * @date 2020-04-30
 */
@Slf4j
public class ApplicationContext {

    @Data
    public static class Context {
        private String name;
    }

    private static final ThreadLocal<Context> threadLocal = ThreadLocal.withInitial(Context::new);

    public static ApplicationContext getApplicationContext() {
        return ContextHolder.context;
    }

    public static Context getContext() {
        return threadLocal.get();
    }

    static class ContextHolder {
        private static final ApplicationContext context = new ApplicationContext();
    }
}
